package com.laicunba.service.accounting.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.pojo.UserPackageItemDO;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.FinancingTargetTagService;
import com.laicunba.service.SaleService;
import com.laicunba.service.UserPackageItemService;
import com.laicunba.service.UserService;
import com.laicunba.service.AccountService.ACCOUNTING_SUBJECT;
import com.laicunba.service.accounting.AccountingException;
import com.laicunba.service.accounting.AccountingTemplate;
import com.laicunba.service.accounting.ExperienceActivityService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.StringUtil;

@Service
public class ExperienceActivityServiceImpl extends AccountingTemplate implements ExperienceActivityService {
	private static final Logger LOG = Logger.getLogger(ExperienceActivityServiceImpl.class);
	@Autowired
	private UserPackageItemService userPackageItemService;
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private SaleService saleService;
	@Autowired
	private UserService userService;

	@Transactional
	@Override
	public boolean doAutoBuy(String packageItemId) {
		// 核对红包是否有效
		AssertUtil.assertNotNull(packageItemId, "PARAM_ERROR");
		UserPackageItemDO packageItem = userPackageItemService.get(UserPackageItemDO.class, packageItemId);
		AssertUtil.assertFalse(packageItem.isUsed(), "PACKAGE_ITEM_HAS_USED");
		AssertUtil.assertTrue(packageItem.getBeginTime() == null || packageItem.getBeginTime().before(new Date()), "BEGIN_TIME_ERROR");
		AssertUtil.assertTrue(packageItem.getEndTime() == null || packageItem.getEndTime().after(new Date()), "END_TIME_ERROR");

		UserDO user = userService.get(UserDO.class, packageItem.getUserId());
		AssertUtil.assertNotNull(user, "USER_NOT_EXIST");

		BigDecimal buyAmount = packageItem.getAmount();
		AssertUtil.assertNotNull(buyAmount);
		AssertUtil.assertTrue(buyAmount.compareTo(BigDecimal.ZERO) > 0, "AMOUNT_ERROR");
		// 找到相应的标的

		FinancingTargetDO targetDO = financingTargetService.find(packageItem.getType(), buyAmount);
		AssertUtil.assertNotNull(targetDO, "CANT_FIND_TARGET");

		// 置为可用
		packageItem.setUsed(true);
		packageItem.setUsedTime(new Date());
		AssertUtil.assertTrue(userPackageItemService.updateSkipCheckOwner(packageItem), "UPDATE_PACKAGE_ITEM_ERROR");

		String certificateId = StringUtil.genGUID();
		// 进入购买过程
		SaleService.MakeFinancingTargetBidParam lockParam = new SaleService.MakeFinancingTargetBidParam();
		lockParam.setCertificateId(certificateId);
		lockParam.setFinancingTargetId(targetDO.getId());
		lockParam.setBuyAmount(buyAmount);
		lockParam.setBuyUserId(packageItem.getUserId());
		lockParam.setSummary("注册体验活动,到期只提息,不提本金!");
		lockParam.setWithdrawType(packageItem.getType());
		AssertUtil.assertTrue(saleService.makeFinancingTargetBid(lockParam), "LOCK_ERROR");

		SaleService.PayedFinancingTargetParam payedParam = new SaleService.PayedFinancingTargetParam();
		payedParam.setCertificateId(certificateId);
		payedParam.setPayMark("ACTIVITY");
		AssertUtil.assertTrue(saleService.payedFinancingTarget(payedParam), "PAYED_ERROR");

//		StringBuffer totalSignBuffer = new StringBuffer();
//		// 用户借记
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_BEFORE_ACCOUNTING_ERROR);
//		String sign = makeUserDr(user.getId(), ACCOUNTING_SUBJECT.USER_CURRENCY_TYJ, buyAmount, certificateId, "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertNotNull(sign);
//		// 用户贷记
//		sign = makeUserCr(user.getId(), ACCOUNTING_SUBJECT.USER_FINANCING_TARGET_BID, buyAmount, certificateId, "", "");
//		totalSignBuffer.append(sign);
//		// 平台借记
//		sign = makePlatformDr(ACCOUNTING_SUBJECT.CURRENCY_TYJ, buyAmount, certificateId, "", "");
//		totalSignBuffer.append(sign);
//		// 平台贷记
//		sign = makePlatformCr(ACCOUNTING_SUBJECT.FINANCING_TARGET_BID, buyAmount, certificateId, "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_AFTER_ACCOUNTING_ERROR);

		return true;
	}

}
